#include "WorldCallbacks.h"


QueryCallback::QueryCallback(int nHandler):
m_nScriptHandler(0)
{
	if (m_nScriptHandler)
	{
		CCScriptEngineManager::sharedManager()->getScriptEngine()->removeLuaHandler(m_nScriptHandler);
	}
	m_nScriptHandler = nHandler;
    CCLOG("m_nScriptHandler = %d", m_nScriptHandler);
}
QueryCallback::~QueryCallback() 
{
	if (m_nScriptHandler)
	{
		CCScriptEngineManager::sharedManager()->getScriptEngine()->removeLuaHandler(m_nScriptHandler);
	}
	m_nScriptHandler = 0;
	CCLOG("QueryCallback destructed ... ");
}

/// Called for each fixture found in the query AABB.
/// @return false to terminate the query.
bool QueryCallback::ReportFixture(b2Fixture* fixture)
{
	CCScriptEngineProtocol* pScriptEngine =  CCScriptEngineManager::sharedManager()->getScriptEngine();
    if (fixture) 
    {
        tolua_pushusertype(pScriptEngine->getLuaState(), fixture, "b2Fixture"); 
        pScriptEngine->executeFunctionByHandler(m_nScriptHandler, 1);
    }
    else 
    {
        CCLOG("QueryCallback::ReportFixture : fixture = null");
    }
	return true;
}